<template>
  <div class="flex h-[290px] justify-between">
    <div class="flex h-full w-[320px] flex-col border-[1px] border-solid border-slate-200 p-[8px]">
      <el-input
        v-model="leftFilterText"
        placeholder="请输入姓名或部门"
        size="small"
        suffix-icon="el-icon-search"
      ></el-input>
      <el-tree
        ref="leftTree"
        :data="leftTreedata"
        show-checkbox
        node-key="id"
        :props="defaultProps"
        :default-expand-all="true"
        class="mt-[8px] flex-1 overflow-y-scroll"
        check-strictly
        check-on-click-node
        :filter-node-method="filterLeftNode"
      >
        <span slot-scope="{ node, data }">
          <span>{{ node.label }}</span>
        </span>
      </el-tree>
    </div>
    <div class="flex h-full flex-col items-center justify-center px-[12px]">
      <span class="btnMove" @click="leftChange"><i class="el-icon-arrow-right"></i></span>
      <span class="btnMove" @click="rightChange"><i class="el-icon-arrow-left"></i></span>
    </div>
    <div class="flex h-full w-[320px] flex-col border-[1px] border-solid border-slate-200 p-[8px]">
      <el-input
        v-model="rightFilterText"
        placeholder="请输入关键词"
        size="small"
        suffix-icon="el-icon-search"
      ></el-input>
      <el-checkbox-group v-model="rightSelected" class="overflow-y-scroll">
        <el-checkbox v-for="item in rightTreedata" :key="item.id" :label="item.id" class="my-[8px] block">{{
          item.label
        }}</el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>

<script>
import vModelMixin from "@/mixin/v-model";
import { systemUserListUserDepartment } from "@/api/user";
import { convertToTree } from "@/util";
const { clone } = require("xe-utils");
export default {
  name: "CheckPersonnelSelection",
  mixins: [vModelMixin],
  props: {
    initData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      leftTreedata: [],
      rightTreedata: [],
      defaultProps: {
        children: "children",
        label: "label",
      },
      leftFilterText: "",
      rightFilterText: "",
      rightSelected: [],
    };
  },
  watch: {
    leftFilterText(val) {
      this.$refs.leftTree.filter(val);
    },
  },
  created() {
    this.onDrawerOpen();
  },
  methods: {
    onDrawerOpen() {
      systemUserListUserDepartment({}).then((data) => {
        const list = (data || []).map((item) => {
          return {
            parentId: item.parentId === "department_0" ? null : item.parentId,
            id: item.id,
            label: item.name,
            isDepartment: item.isDepartment,
            disabled: item.isDepartment ? true : false,
          };
        });
        // this.leftTreedata = convertToTree(list);
        //初始化树data,checkboxData
        const leftInit = list.filter((f) => !this.initData?.some((s) => s == f.id));
        const rightInit = list.filter((f) => this.initData?.some((s) => s == f.id));
        this.leftTreedata = convertToTree(leftInit);
        this.rightTreedata = rightInit;
      });
    },
    leftChange() {
      const checkedNodes = this.$refs.leftTree.getCheckedNodes();
      if (checkedNodes && checkedNodes.length) {
        this.rightTreedata = this.rightTreedata.concat(checkedNodes);
        // 移到右侧框后、左侧树删除选中节点
        checkedNodes.forEach((item) => {
          this.$refs.leftTree.remove(item);
        });
      } else {
        this.$message.warning("请选择人员");
      }
    },
    filterLeftNode(value, data) {
      if (!value) return true;
      return data.label ? data.label.indexOf(value) !== -1 : false;
    },
    rightChange() {
      if (!this.rightSelected.length) {
        this.$message.warning("请选择人员");
        return;
      }
      this.rightTreedata.forEach((item) => {
        if (this.rightSelected.includes(item.id)) {
          this.$refs.leftTree.append(item, item.parentId);
        }
      });
      this.rightTreedata = this.rightTreedata.filter((v) => {
        return !this.rightSelected.includes(v.id);
      });
    },
    getSelectNode() {
      return this.rightSelected;
    },
  },
};
</script>

<style lang="scss" scoped>
.btnMove {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 24px;
  height: 24px;
  color: rgb(0 0 0 / 0.26);
  cursor: pointer;
  background: #f8f9fa;
  border: 1px solid #bac3d3;
  border-radius: 3px;
  transition: all 0.3s;

  & + .btnMove {
    margin-top: 16px;
  }

  &:hover {
    color: #fff;
    background: #1d7ff2;
    border-color: #bac3d3;
  }
}
</style>
